import {createRouter, createWebHistory} from 'vue-router'
import Myorder from "../views/Center/Myorder/Myorder";
import Grouporder from "../views/Center/Grouporder/Grouporder";


const routes = [
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/home',
        name: 'Home',
        component: () => import('../views/Home/Home'),
        meta: {show: true}
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/Login/Login'),
        meta: {show: false}
    },
    {
        path: '/register',
        name: 'Register',
        component: () => import('../views/Register/Register'),
        meta: {show: false}
    },
    {
        path: '/search/:keyword?',
        name: 'Search',
        component: () => import('../views/Search/Search'),
        meta: {show: true},
        props: true
    },
    {
        path: '/detail/:skuId',
        name: 'Detail',
        component: () => import('../views/Detail/Detail'),
        meta: {show: false}
    },
    {
        path: '/addcartsuccess',
        name: 'Addcartsuccess',
        component: () => import('../views/AddCartSuccess/AddCartSuccess'),
        meta: {show: true}
    },
    {
        path: '/shopcart',
        name: 'Shopcart',
        component: () => import('../views/ShopCart/ShopCart'),
        meta: {show: true}
    },
    {
        path: '/trade',
        name: 'Trade',
        component: () => import('../views/Trade/Trade'),
        meta: {show: true}
    },
    {
        path: '/pay',
        name: 'Pay',
        component: () => import('../views/Pay/Pay'),
        meta: {show: true}
    },
    {
        path: '/paysuccess',
        name: 'PaySuccess',
        component: () => import('../views/PaySuccess/PaySuccess'),
        meta: {show: true}
    },
    {
        path: '/center',
        name: 'Center',
        component: () => import('../views/Center/Center'),
        meta: {show: true},
        children: [
            // 路由重定向，默认显示的子页面是哪一个
            {
                path: '/center',
                redirect: '/center/myorder'
            },
            {
                path: 'myorder',
                component: Myorder
            },
            {
                path: 'grouporder',
                component: Grouporder
            },
        ]
    },
    // echarts实例
    {
        path: '/backendbaseecharts',
        name: 'Backendbaseecharts',
        component: () => import('../views/BackendBaseEcharts/Backendbaseecharts'),
        meta: {show: false}
    },
    // elementplus响应式布局实例
    {
        path: '/xiangying',
        name: 'Xiangying',
        component: () => import('../views/XiangYing/Xiangying'),
        meta: {show: false}
    },
    // elementplus轮播图实例
    {
        path: '/carouselbaseelementplus',
        name: 'Carouselbaseelementplus',
        component: () => import('../views/CarouselBaseElementplus/Carouselbaseelementplus'),
        meta: {show: false}
    },

]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    // mode:history,
    routes,
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition;
        } else {
            return {top: 0};
        }
    },
});

// 全局导航守卫(在路由跳转之前进行判断)
router.beforeEach((to, from, next) => {
// to:你要往哪里跳转;from:你从哪里开始跳转的;next:直接放行的路由
    let token = localStorage.getItem('TOKEN')
    if (token) {
        if (to.path == '/login') {
            next('/home')
        } else {
            next()
        }
    } else {
        next()
    }
})

export default router
